﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using SqlSugar;

namespace DAL.User
{
    public class UserDao : BaseDaoPro<Models.User>, IUserDao
    {
        public override bool Delete(int[] ids)
        {

            try
            {
                db.Ado.BeginTran();

                foreach (int id in ids)
                {
                    // 删除该用户所发布需求下的应征者
                    db.Deleteable<Models.ProjectArtists>()
                        .Where(a => SqlFunc.Subqueryable<Models.Projects>().Where(p => p.ProjectID == a.ProjectID && p.OwnerID == id).Any())
                        .ExecuteCommandHasChange();

                    // 删除该用户的应征
                    db.Deleteable<Models.ProjectArtists>()
                        .Where(it => it.OwnerID == id)
                        .ExecuteCommandHasChange();

                    // 删除该用户所发布的需求
                    db.Deleteable<Models.Projects>()
                        .Where(it => it.OwnerID == id)
                        .ExecuteCommandHasChange();

                    // 删除该用户所发布作品的 所有点赞
                    db.Deleteable<Models.ArtworkLikes>()
                        .Where(it => SqlFunc.Subqueryable<Models.Artworks>().Where(a=>a.ArtworkID == it.ArtworkID && a.OwnerID == id).Any())
                        .ExecuteCommandHasChange();

                    // 删除该用户的点赞
                    db.Deleteable<Models.ArtworkLikes>()
                        .Where(it => it.OwnerID == id)
                        .ExecuteCommandHasChange();

                    // 删除该用户所发布的作品
                    db.Deleteable<Models.Artworks>()
                        .Where(it => it.OwnerID == id)
                        .ExecuteCommandHasChange();

                    // 删除该用户的接稿信息
                    db.Deleteable<Models.ResumeInfo>()
                        .Where(it => it.OwnerID == id)
                        .ExecuteCommandHasChange();


                    //删除该用户的信息
                    db.Deleteable<Models.UserInfo>()
                        .Where(it => it.UserID == id)
                        .ExecuteCommandHasChange();

                    // 删除该用户
                    db.Deleteable<Models.User>()
                        .Where(it => it.UserID == id)
                        .ExecuteCommandHasChange();
                }

                db.Ado.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                db.Ado.RollbackTran();
                return false;
            }


        }

        public override List<Models.User> QueryForList(List<IConditionalModel> searchParams, ref int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            return db.Queryable<Models.User>()
                .Mapper(it => it.UserInfo, it => it.UserID, it => it.UserInfo.UserID)
                .Mapper(it => it.ResumeInfo, it => it.UserID, it => it.ResumeInfo.OwnerID)
                .Mapper(it => it.Artworks, it => it.UserID, it => it.Artworks.First().OwnerID)
                .Mapper(it => { it.Artworks = it.Artworks.OrderByDescending(x => x.OrderNum).ThenByDescending(x => x.CreatedAt).Take(4).ToList(); })
                .Where(searchParams)
                .ToPageList(pageIndex, pageSize, ref totalCount);
        }
        public override List<Models.User> QueryForList(List<IConditionalModel> searchParams, string orderBy, ref int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            return db.Queryable<Models.User>()
                .Mapper(it => it.UserInfo, it => it.UserID, it => it.UserInfo.UserID)
                .Mapper(it => it.ResumeInfo, it => it.UserID, it => it.ResumeInfo.OwnerID)
                .Mapper(it => it.Artworks, it => it.UserID, it => it.Artworks.First().OwnerID)
                .Mapper(it => { it.Artworks = it.Artworks.OrderByDescending(x => x.OrderNum).ThenByDescending(x => x.CreatedAt).Take(4).ToList(); })
                .Where(searchParams)
                .OrderBy(orderBy)
                .ToPageList(pageIndex, pageSize, ref totalCount);
        }

        public List<Models.User> GetList(ref int totalCount, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            return db.Queryable<Models.User>()
                .Mapper(it => it.UserInfo, it => it.UserID, it => it.UserInfo.UserID)
                .Mapper(it => it.ResumeInfo, it => it.UserID, it => it.ResumeInfo.OwnerID)
                .Mapper(it => it.Artworks, it => it.UserID, it => it.Artworks.First().OwnerID)
                .Mapper(it => { it.Artworks = it.Artworks.OrderByDescending(x => x.OrderNum).ThenByDescending(x=>x.CreatedAt).Take(4).ToList(); })
                .OrderBy(orderBy)
                .ToPageList(pageIndex, pageSize, ref totalCount);
        }

        public List<Models.User> GetList(ref int totalCount, string[] tags, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            Expressionable<Models.User> exp = new Expressionable<Models.User>();
            foreach (var tag in tags)
            {
                exp.Or(it => it.ResumeInfo.Tag.Contains(tag));
            }
            return db.Queryable<Models.User>()
                .Mapper(it => it.UserInfo, it => it.UserID, it => it.UserInfo.UserID)
                .Mapper(it => it.ResumeInfo, it => it.UserID, it => it.ResumeInfo.OwnerID)
                .Mapper(it => it.Artworks, it => it.UserID, it => it.Artworks.First().OwnerID)
                .Mapper(it => { it.Artworks = it.Artworks.OrderByDescending(x => x.OrderNum).ThenByDescending(x => x.CreatedAt).Take(4).ToList(); })
                .Where(exp.ToExpression())
                .OrderBy(orderBy)
                .ToPageList(pageIndex, pageSize, ref totalCount);
        }

        public Models.ResumeInfo QueryResumeInfo(Models.User user)
        {
            return db.Queryable<Models.ResumeInfo>()
                .Mapper(it => it.Artworks, it => it.OwnerID, it => it.Artworks.First().OwnerID)
                .Mapper(it => { it.Artworks = it.Artworks.OrderByDescending(x => x.OrderNum).ThenByDescending(x => x.CreatedAt).Take(4).ToList(); })
                .First(it => it.OwnerID == user.UserID);
        }

        public Models.User QueryUserByUserName(string username)
        {
            return db.Queryable<Models.User>()
                .First(it => it.UserName == username);
        }

        public Models.User QueryUserByUserNameAndPwd(string username, string pwd)
        {
            return db.Queryable<Models.User>()
                .First(it => it.UserName == username && it.UserPwd == pwd);
        }

        public Models.UserInfo QueryUserInfo(Models.User user)
        {
            return db.Queryable<Models.UserInfo>()
                .First(it => it.UserID == user.UserID);
        }

        public void saveResumeInfo(ResumeInfo resumeInfo)
        {
            db.Insertable<Models.ResumeInfo>(resumeInfo).ExecuteCommandIdentityIntoEntity();
        }

        public void saveUser(Models.User user)
        {
            db.Insertable<Models.User>(user).ExecuteCommandIdentityIntoEntity();
        }

        public void saveUserInfo(Models.UserInfo userInfo)
        {
            db.Insertable<Models.UserInfo>(userInfo).ExecuteCommandIdentityIntoEntity();
        }

        public bool updateResumeInfo(ResumeInfo resumeInfo)
        {
            return db.Updateable<Models.ResumeInfo>(resumeInfo).ExecuteCommandHasChange();
        }

        public bool updateUser(Models.User user)
        {
            return db.Updateable<Models.User>(user).ExecuteCommandHasChange();
        }

        public bool updateUserInfo(Models.UserInfo userInfo)
        {
            return db.Updateable<Models.UserInfo>(userInfo).ExecuteCommandHasChange();
        }
    }
}
